package server;

import tools.FileoutputUtil;
import tools.StringUtil;

import java.io.*;
import java.util.Properties;

public class ServerProperties {
    private static Properties props;
    public static String[] toload;

    public static void loadProperties() {
        for (String s : ServerProperties.toload) {
            try {
                InputStream in = new FileInputStream(s);
                BufferedReader bf = new BufferedReader(new InputStreamReader(in, StringUtil.codeString(s)));
                ServerProperties.props.load(bf);
                bf.close();
            } catch (IOException ex) {
                FileoutputUtil.logToFile(ex.getLocalizedMessage().split(".ini")[0] + ".ini", "\r\n");
                System.out.println("服务端缺少运行的配置文件,已经自动生成空白配置“" + ex.getLocalizedMessage().split(".ini")[0] + ".ini”文件，请重新启动服务端。");
                System.exit(0);
            }
        }
    }

    public static void setProperty(String prop, String newInf) {
        ServerProperties.props.setProperty(prop, newInf);
    }

    public static void setProperty(String prop, boolean newInf) {
        ServerProperties.props.setProperty(prop, String.valueOf(newInf));
    }

    public static void setProperty(String prop, byte newInf) {
        ServerProperties.props.setProperty(prop, String.valueOf((int) newInf));
    }

    public static void setProperty(String prop, short newInf) {
        ServerProperties.props.setProperty(prop, String.valueOf((int) newInf));
    }

    public static void setProperty(String prop, int newInf) {
        ServerProperties.props.setProperty(prop, String.valueOf(newInf));
    }

    public static void setProperty(String prop, long newInf) {
        ServerProperties.props.setProperty(prop, String.valueOf(newInf));
    }

    public static void removeProperty(String prop) {
        ServerProperties.props.remove(prop);
    }

    public static String getProperty(String s) {
        return ServerProperties.props.getProperty(s);
    }

    public static String getProperty(String s, String def) {
        return ServerProperties.props.getProperty(s, def);
    }

    public static boolean getProperty(String s, boolean def) {
        return getProperty(s, def ? "true" : "false").equalsIgnoreCase("true");
    }

    public static byte getProperty(String s, byte def) {
        String property = ServerProperties.props.getProperty(s);
        if (property != null) {
            return Byte.parseByte(property);
        }
        return def;
    }

    public static short getProperty(String s, short def) {
        String property = ServerProperties.props.getProperty(s);
        if (property != null) {
            return Short.parseShort(property);
        }
        return def;
    }

    public static int getProperty(String s, int def) {
        String property = ServerProperties.props.getProperty(s);
        if (property != null) {
            return Integer.parseInt(property);
        }
        return def;
    }

    public static long getProperty(String s, long def) {
        String property = ServerProperties.props.getProperty(s);
        if (property != null) {
            return Long.parseLong(property);
        }
        return def;
    }

    public static void clear() {
        ServerProperties.props.clear();
        loadProperties();
        clear1();
    }

    public static void clear1() {
    }

    static {
        props = new Properties();
        //toload = new String[] { "Load\\cc_ms.ini", "Load\\事件载入.ini", "Load\\坐骑椅子.ini", "Load\\捉鬼任务.ini", "Load\\经验倍数.ini", "Load\\转生属性.ini", "Load\\挂机功能.ini", "Load\\宠物属性.ini", "Load\\地图效果.ini", "Load\\mob\\黑龙线程.ini", "Load\\mob\\蘑辣插线程.ini", "Load\\mob\\捉鬼线程.ini", "Load\\mob\\怪物挑战所需人数.ini", "Load\\rpg\\蘑菇活动.ini", "Load\\mob\\扎昆线程.ini" };
        toload = new String[]{"Load/cc_ms.ini", "Load/事件载入.ini", "Load/坐骑椅子.ini", "Load/捉鬼任务.ini", "Load/经验倍数.ini", "Load/转生属性.ini", "Load/挂机功能.ini", "Load/宠物属性.ini", "Load/地图效果.ini", "Load/mob/黑龙线程.ini", "Load/mob/蘑辣插线程.ini", "Load/mob/捉鬼线程.ini", "Load/mob/怪物挑战所需人数.ini", "Load/rpg/蘑菇活动.ini", "Load/mob/扎昆线程.ini"};
        loadProperties();
    }
}
